package com.ece.devirtualizus.Detectors;

import android.hardware.Sensor;
import android.hardware.SensorEvent;
import android.hardware.SensorEventListener;
import android.hardware.SensorManager;
import android.location.Location;

import com.ece.devirtualizus.Activities.MainActivity;
import com.ece.devirtualizus.ModelDto.User;

public class StepDetector implements DetectorInterface, SensorEventListener {

	private MainActivity _main;
	private final static String TAG = "StepDetector";

	private float mLimit = 20;
	private float mLastValues[] = new float[3 * 2];
	private float mScale[] = new float[2];
	private float mYOffset;

	private float mLastDirections[] = new float[3 * 2];
	private float mLastExtremes[][] = { new float[3 * 2], new float[3 * 2] };
	private float mLastDiff[] = new float[3 * 2];
	private int mLastMatch = -1;
	private User user;

	public StepDetector(MainActivity m, User u) {
		user = u;
		_main = m;
		int h = 480; // TODO: remove this constant
		mYOffset = h * 0.5f;
		mScale[0] = -(h * 0.5f * (1.0f / (SensorManager.STANDARD_GRAVITY * 2)));
		mScale[1] = -(h * 0.5f * (1.0f / (SensorManager.MAGNETIC_FIELD_EARTH_MAX)));
	}

	@Override
	public void onAccuracyChanged(Sensor arg0, int arg1) {
		// TODO Auto-generated method stub

	}

	@Override
	public void onSensorChanged(SensorEvent arg0) {
		if (arg0.sensor.getType() == Sensor.TYPE_ACCELEROMETER) {
			synchronized (this) {
				this.getDetectorValues(arg0);
			}
		}
	}

	@Override
	public int getDetectorValues(SensorEvent event) {

//		float vSum = 0;
//		for (int i = 0; i < 3; i++) {
//			final float v = mYOffset + event.values[i] * mScale[1];
//			vSum += v;
//		}
//		int k = 0;
//		float v = vSum / 3;
//
//		float direction = (v > mLastValues[k] ? 1 : (v < mLastValues[k] ? -1
//				: 0));
//		if (direction == -mLastDirections[k]) {
//			// Direction changed
//			int extType = (direction > 0 ? 0 : 1); // minumum or maximum?
//			mLastExtremes[extType][k] = mLastValues[k];
//			float diff = Math.abs(mLastExtremes[extType][k]
//					- mLastExtremes[1 - extType][k]);
//
//			if (diff > mLimit) {
//
//				boolean isAlmostAsLargeAsPrevious = diff > (mLastDiff[k] * 2 / 3);
//				boolean isPreviousLargeEnough = mLastDiff[k] > (diff / 3);
//				boolean isNotContra = (mLastMatch != 1 - extType);
//
//				if (isAlmostAsLargeAsPrevious && isPreviousLargeEnough
//						&& isNotContra) {
//					Log.d("************STEP DETECTOR************", "ACTION PAS");
////					_main.incrementStepNumber();
////					Log.i(TAG, "step:" + user.getSteps());
//					mLastMatch = extType;
//				} else {
//					mLastMatch = -1;
//				}
//			}
//			mLastDiff[k] = diff;
//		}
//		mLastDirections[k] = direction;
//		mLastValues[k] = v;
//
//		return user.getSteps();
		return 0;
	}

	@Override
	public void startDetection() {
		// TODO Auto-generated method stub

	}

	@Override
	public void stopDetection() {
		// TODO Auto-generated method stub

	}

	@Override
	public int getDetectorValues(Location location) {
		// TODO Auto-generated method stub
		return 0;
	}


}
